package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Connectivity.ConnectionManager;

public class EmployeeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 57538492916645959L;

	/**
	 * Constructor of the object.
	 */
	public EmployeeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 
		 String flag = "";
		 String cust_id = request.getParameter("custId")!=null?request.getParameter("custId"):"";
		 String customer_name = request.getParameter("name")!=null?request.getParameter("name"):"";
		 String area = request.getParameter("branch")!=null?request.getParameter("branch"):"";
		 String phone_no1 = request.getParameter("telephone1")!=null?request.getParameter("telephone1"):"";
		 String phone_no2 = request.getParameter("telephone2")!=null?request.getParameter("telephone2"):"";
		 String mobile_no = request.getParameter("mobileno")!=null?request.getParameter("mobileno"):"";
		 String address1 = request.getParameter("address")!=null?request.getParameter("address"):"";
		 String email_id = request.getParameter("email")!=null?request.getParameter("email"):"";
		 String contactperson1 = request.getParameter("cust1")!=null?request.getParameter("cust1"):"";
		 String contactperson2 = request.getParameter("cust2")!=null?request.getParameter("cust2"):"";
		 String mobilecontact2 = request.getParameter("cust2mobile")!=null?request.getParameter("cust2mobile"):""; 
		 String mobilecontact1 = request.getParameter("cust1mobile")!=null?request.getParameter("cust1mobile"):"";
		 String submit = request.getParameter("submit")!=null?request.getParameter("submit"):"";
		// Date date_of_commencement="(select current_date)";
		 String currency_symbol="Rupees";
		 //String creation_date="(select current_date)";
		 HttpSession session = request.getSession(true);
		 String created_by=""+(session.getAttribute("uname"));
	System.out.println("cust_id"+cust_id);
	System.out.println("customer_name"+customer_name);
	System.out.println("area"+area);
	System.out.println("phone_no1"+phone_no1);
	System.out.println("phone_no2"+phone_no2);
	System.out.println("mobile_no"+mobile_no);
	System.out.println("address1"+address1);
	System.out.println("email_id"+email_id);
	System.out.println("contactperson1"+contactperson1);
	System.out.println("contactperson2"+contactperson2);
	System.out.println("submit"+submit);
	
	try
	{	    
 
	    Connection con=null;
	    flag=request.getParameter("flag");
	    System.out.println("flag:"+flag);
		
		con=ConnectionManager.getConnection();
		if(  request.getParameter("submit").equalsIgnoreCase("create"))
		{
		String query="Insert into customer_master(customer_id,customer_name,address1,phone_no1,phone_no2,mobile_no,email_id";
		query=query+",contactperson1,contactperson2,mobilecontact1,mobilecontact2, currency_symbol";
		query=query+",created_by,area,status) values(nextval('customer_master_seq'),?,?,?,?,?,?,?,?,?,?,?,?,?,'V')";
		PreparedStatement prst=con.prepareStatement(query);
	 	prst.setString(1,customer_name);
		prst.setString(2,address1);
		prst.setString(3,phone_no1);
		prst.setString(4,phone_no2);
		prst.setString(5,mobile_no);
		prst.setString(6,email_id);
		prst.setString(7,contactperson1);
		prst.setString(8,contactperson2);
		prst.setString(9,mobilecontact1);
		prst.setString(10,mobilecontact2);
		 
		prst.setString(11,currency_symbol);
		prst.setString(12,created_by);
		prst.setString(13,area);
		prst.execute();
	          
	    response.sendRedirect("/Genius/form/customer/CustRegistration.jsp?status=I"); //logged-in page  
		}
		else if(  request.getParameter("submit").equalsIgnoreCase("update"))
			{
			String query="update customer_master set customer_name='"+customer_name+"',address1='"+address1+"',phone_no1='"+phone_no1+"'";
			query=query+",phone_no2='"+phone_no2+"',mobile_no='"+mobile_no+"',email_id='"+email_id+"'";
			query=query+",contactperson1='"+contactperson1+"',contactperson2='"+contactperson2+"'";
			query=query+",mobilecontact1='"+mobilecontact1+"',mobilecontact2='"+mobilecontact2+"'";
			query=query+",area='"+area+"' where customer_id="+cust_id+" and status='V'";
			PreparedStatement prst=con.prepareStatement(query);
			prst.executeUpdate();
			
			
			
			
			 response.sendRedirect("/Genius/form/customer/CustDisplay.jsp?flag=U&status=update&cust_id="+cust_id);
				}
		else if(  request.getParameter("submit").equalsIgnoreCase("delete") )
				{
			String query="UPDATE customer_master set status='I'";
			query=query+"where customer_id = ";
			PreparedStatement prst=con.prepareStatement(query);
			prst.executeUpdate();
			response.sendRedirect("/Genius/form/customer/CustRegistration.jsp?status=D");
				}
		else if(  request.getParameter("submit").equalsIgnoreCase("BACK") )
		{
	 
	response.sendRedirect("/Genius/form/customer/CustListReport.jsp");
		}
	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String groupcode=null;
		String groupname=null;
	
	try
	{	    

		//groupcode=123&groupname=23423
		groupcode=(request.getParameter("groupcode"));
		groupname=(request.getParameter("groupname"));
	    Connection con=null;
	
		
		con=ConnectionManager.getConnection();
		String query="Insert into item_group_master(group_id,group_code,group_name,status) values(nextval('item_group_master_seq'),?,?,'V')";
		PreparedStatement prst=con.prepareStatement(query);
	 	prst.setString(1,groupcode);
		prst.setString(2,groupname);
	    prst.execute();
	          
	    response.sendRedirect("/Genius/form/customer/CustRegistration.jsp"); //logged-in page  
	 
	     
	} 
			
			
	catch (Throwable theException) 	    
	{
	     System.out.println(theException); 
	}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
